package org.user.service;

import feign.FeignException;
import io.seata.core.context.RootContext;
import io.seata.core.exception.RmTransactionException;
import io.seata.core.exception.TmTransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import org.user.bean.UserBean;
import org.user.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.user.dynamicFeignClient.DynamicFeignClient;
import org.user.orderfallback.OrderServiceApiFegin;

import java.net.BindException;
import java.sql.SQLException;

@Service("userService")
public class UserServiceImp implements  UserService{

    @Autowired
    UserDao userDao;

    @Autowired
    DynamicFeignClient<OrderServiceApiFegin> client;


    @Override
    @GlobalTransactional(name="fsm-se",timeoutMills = 10000,rollbackFor = {Exception.class, SQLException.class, TmTransactionException.class,
            RmTransactionException.class, BindException.class,ArithmeticException.class, FeignException.class})
    public void insertUser(UserBean userBean){

        String xid = RootContext.getXID();
        System.out.println("用户="+xid);
        userDao.insertUser(userBean);
        String service_name="orderServer8082";
        OrderServiceApiFegin orderServiceApiFegin=client.GetFeignClient(OrderServiceApiFegin.class,service_name);
        System.out.println(orderServiceApiFegin.insertOrder(xid));
    }
}
